package com.gitee.l0km.xthrift.mq.exception;

import java.io.PrintStream;
import java.io.PrintWriter;

import com.google.common.base.Preconditions;

/**
 * @author guyadong
 * @since 1.5.0
 *
 */
public class InvocationRuntimeException extends RuntimeException {
    private static final long serialVersionUID = 1L;
    protected int type;
    protected String message;
    protected String causeClass;
    protected String causeFields;
    protected String serviceStackTraceMessage;

    public InvocationRuntimeException(Throwable cause) {
		super(cause);
	}

	/**
     * @param cause
     */
    public InvocationRuntimeException(ResponseException cause) {
        this.type = cause.type;
        this.message = cause.message;
        this.causeClass=cause.causeClass;
        this.causeFields = cause.causeFields;
        this.serviceStackTraceMessage = cause.serviceStackTraceMessage;
    }

    /**
     * print stack trace message from service to {@link System#err}
     * @see #printStackTrace()
     */
    public void printServiceStackTrace() {
        printServiceStackTrace(System.err);
    }

    /**
     * @param s
     * @see #printServiceStackTrace()
     * @see #printStackTrace(PrintStream)
     * @throws NullPointerException s is {@code null}
     */
    public void printServiceStackTrace(PrintStream s) {
        synchronized (Preconditions.checkNotNull(s)) {
            s.println(serviceStackTraceMessage);
        }
    }

    /**
     * @param s
     * @see #printServiceStackTrace()
     * @see #printStackTrace(PrintWriter)
     * @throws NullPointerException s is {@code null}
     */
    public void printServiceStackTrace(PrintWriter s) {
        synchronized (Preconditions.checkNotNull(s)) {
            s.println(serviceStackTraceMessage);
        }
    }
    /** return error message from service */
    @Override
    public String getMessage() {
        return message;
    }
    /** return cause exception class name */
    public String getCauseClass() {
        return causeClass;
    }
    /** return stack trace message from service */
    public String getServiceStackTraceMessage() {
        return serviceStackTraceMessage;
    }
    /** return exception type */
    public int getType() {
        return type;
    }
    /** 
     * return declared field values JSON string of cause <br>
     */
    public String getCauseFields() {
        return causeFields;
    }
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append( getClass().getSimpleName());
        builder.append(" [type=");
        builder.append(type);
        builder.append(", message=");
        builder.append(message);
        builder.append(", causeClass=");
        builder.append(causeClass);
        builder.append(", causeFields=");
        builder.append(causeFields);
        builder.append(", serviceStackTraceMessage=");
        builder.append(serviceStackTraceMessage);
        builder.append("]");
        return builder.toString();
    }
}
